/**
 * Copyright 2011 FeedDreamwork SIG
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.feeddreamwork;

import java.util.*;

import org.feeddreamwork.processors.*;

import net.sf.jsr107cache.*;

public final class FeedResultCache {
	private final static String CACHE_PREFIX = "FEEDRESULT_";

	public static void put(FeedResult source, int expirationTime) {
		Utils.throwIfNull(source);
		MiniCache.put(CACHE_PREFIX + source.getFeedId(), source, expirationTime);
	}

	public static void put(FeedInfo feedInfo) throws FeedProcessException {
		FeedGenerator feedGenerator = new FeedGenerator(feedInfo);
		FeedResult feedResult = feedGenerator.generate();
		if (feedResult != null)
			MiniCache.put(CACHE_PREFIX + feedInfo.getFeedId(), feedResult);
	}

	public static FeedResult get(String feedId) {
		return (FeedResult) MiniCache.get(CACHE_PREFIX + feedId);
	}

	public static boolean containsKey(String feedId) {
		return MiniCache.containsKey(CACHE_PREFIX + feedId);
	}

	public static void remove(String feedId) {
		MiniCache.remove(CACHE_PREFIX + feedId);
	}

	public static CacheStatistics getCacheStatistics() {
		try {
			Cache cache = CacheManager.getInstance().getCacheFactory()
					.createCache(Collections.EMPTY_MAP);
			return cache.getCacheStatistics();
		} catch (CacheException e) {
			Utils.throwIfDevelopment(e);
			return null;
		}
	}

	private FeedResultCache() {
	}
}
